package com.hmall.order.task;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.hmall.feign.ItemFeign;
import com.hmall.order.pojo.Order;
import com.hmall.order.pojo.OrderDetail;
import com.hmall.order.service.impl.OrderDetailService;
import com.hmall.order.service.impl.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class OrderTask {
    @Autowired
    OrderService orderService;
    @Autowired
    OrderDetailService orderDetailService;
    @Autowired
    ItemFeign itemFeign;



    @Scheduled(cron = "0 0/1 * * * ?" )
    public  void  executeOrderStatus(){
        Date time=new Date(System.currentTimeMillis()-30 * 60 * 1000);
        LambdaQueryWrapper<Order> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Order::getStatus,1);
        queryWrapper.lt(Order::getCreateTime,time);
       List<Order> orderList= orderService.list(queryWrapper);

        for (Order order : orderList) {
            order.setStatus(5);
            orderService.updateById(order);

            LambdaQueryWrapper<OrderDetail> queryWrapper1=new LambdaQueryWrapper<>();
            queryWrapper1.eq(OrderDetail::getOrderId,order.getId());
            List<OrderDetail> orderDetails = orderDetailService.list(queryWrapper1);
            for (OrderDetail orderDetail : orderDetails) {
                itemFeign.updateStock(orderDetail.getItemId(),-orderDetail.getNum());
            }
        }

    }

}
